package com.danieltoms.gcSync.google;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.util.Persistable;

import com.danieltoms.gcSync.util.DateUtil;

public class GoogleDataItem implements Persistable
{
	// todo: store whole state as bits and bit masks
	// TODO NA change to SYNCED
	public static final int NA = 0;
	public static final int BB_ADD = 1; // state set via
	// ContactListListener
	public static final int BB_UPDATED = 2;
	public static final int BB_DELETED = 3;


	private String _googleId = null;
	private String _bbUID = null;
	private int _state = NA;

	// so far only using this for google updated issues, not sure if it should
	// be stored as a string versus a long
	private long _updatedDate = 0;

	// with REMOVED and purge them, as google
	// gets rid of the deleted ones then

	private Vector _emails = null;
	private Vector _phones = null;
	private Vector _addresses = null;
	private Vector _ims = null;

	public GoogleDataItem()
	{

	}

	public void setGoogleId(String googleId)
	{
		this._googleId = googleId;
	}

	public String getGoogleId()
	{
		return _googleId;
	}

	public void setBBUID(String bbUID)
	{
		this._bbUID = bbUID;
	}

	public String getBBUID()
	{
		return _bbUID;
	}

	public void setState(int state)
	{
		this._state = state;
	}

	public int getState()
	{
		return _state;
	}

	public void setUpdatedDateByRFC3339(String updated)
	{
		_updatedDate = DateUtil.parseRFC3339(updated).getTime();

	}

	public void setUpdatedDate(Date d)
	{
		_updatedDate = d.getTime();
	}

	public Date getUpdatedDate()
	{
		return new Date(_updatedDate);
	}

	public String getUpdatedDateAsRFC3339()
	{
		return DateUtil.formatRFC3339(new Date(_updatedDate));
	}

	public Vector getEmails()
	{
		return _emails;
	}

	public int getEmailCount()
	{
		int ret = 0;

		if (_emails != null)
		{
			Enumeration e = _emails.elements();
			if (e != null)
			{
				while (e.hasMoreElements())
				{
					ret++;
					e.nextElement();
				}
			}
		}
		return ret;

	}

	public void setEmails(Vector emails)
	{
		this._emails = emails;
	}

	public Vector getPhones()
	{
		return _phones;
	}

	public void setPhones(Vector phones)
	{
		this._phones = phones;
	}

	public Vector getAddresses()
	{
		return _addresses;
	}

	public void setAddresses(Vector addresses)
	{
		this._addresses = addresses;
	}

	public Vector getTypedVector(Vector v, String type)
	{
		Vector ret = null;
		if (v != null)
		{
			ret = new Vector();

			Enumeration e = v.elements();
			while (e.hasMoreElements())
			{
				GoogleTypedField field = (GoogleTypedField) e.nextElement();

				// if the type is null, this is the primary email, skip it.
				if (field.getType() == null)
					continue;

				if (field.getType().equals(type))
					ret.addElement(field);
			}
		}

		return ret;
	}

	public Vector getIms()
	{
		return _ims;
	}

	public void setIms(Vector ims)
	{
		_ims = ims;
	}
}
